from pushByFeishu import FeiShuMSG
import tushare as ts
import datetime

webhook = f'https://open.feishu.cn/open-apis/bot/v2/hook/82a9207f-79d2-4390-a82e-d4e3c3540509'
# secrets = f"ZRHiGlHCp3W******"  # 签名密钥 没有无需传参
# feishu = FeiShuMSG(token=webhook, secret=secrets, text=message_text)


# 获取股票当前信息
def get_now_jiage(code):
   df = ts.get_realtime_quotes(code)[['name','open','price','pre_close','date','time']]
   return df

# 从txt中读取文件到python列表中
def read_txt_to_list(file_path):
   data_list = []
   try:
      with open(file_path, 'r', encoding='utf-8') as file:
         for line in file:
            line = line.strip()  # 去除行末的换行符和空格
            data_list.append(line)
   except FileNotFoundError:
      print(f"文件 {file_path} 不存在。")
   return data_list

# 根据dataframe拼接单个股票最终显示信息
def get_msg_from_df(df):
   name = df.loc[0, 'name']
   open = df.loc[0, 'open']
   # print(df.loc[0, 'open'])
   price = df.loc[0, 'price']
   # print(df.loc[0, 'price'])
   pre_close = df.loc[0,'pre_close']
   float_open = float(open)
   float_price = float(price)
   float_pre_close = float(pre_close)
   increase_rate_today = ((float_price-float_open)/float_open)*100
   increase_rate_yesterday = ((float_price-float_pre_close)/float_pre_close)*100
   msg = f"股票：{name}的当前价格为{price},当前增长率{round(increase_rate_today,2)}%,涨跌幅度{round(increase_rate_yesterday,2)}%\n"
   return msg
# 输出最开始的欢迎语
def begin_message():
   now = datetime.datetime.now()
   formatted_time =  now.strftime("%Y-%m-%d %H:%M:%S")
   print(f"当前时间为:{formatted_time}")

def get_my_stock(df,buy_price):
   name = df.loc[0, 'name']
   open = df.loc[0, 'open']
   # print(df.loc[0, 'open'])
   price = df.loc[0, 'price']
   # print(df.loc[0, 'price'])
   pre_close = df.loc[0,'pre_close']
   float_open = float(open)
   float_price = float(price)
   float_pre_close = float(pre_close)
   float_buy_price = float(buy_price)
   result = "增长" if float_price-float_buy_price >0 else "下跌"
   increase_rate_buy = ((float_price-float_buy_price)/float_buy_price)*100


   msg = f"您买的股票现在状态是({result}),幅度为{round(increase_rate_buy,2)}%\n"
   return msg



if __name__ == "__main__":
   begin_message()
   send_msg=''
   # file_path = "D:/python_project/toolsproject/stockYongTaiNengYuan/feishu/stock.txt"
   file_path = "/root/pythonProgram/stock.txt"
   result_list = read_txt_to_list(file_path)
   for item in result_list:
      if '|' in item:
         parts = item.split('|')
         df = get_now_jiage(parts[0])
         msg = get_msg_from_df(df)
         msg1 = get_my_stock(df, parts[1])
         send_msg = send_msg+msg+msg1
      else:
         df = get_now_jiage(item)
         # print(df)
         msg = get_msg_from_df(df)
         send_msg =send_msg+msg

   print(send_msg)


#
# df = get_now_jiage('600157')
# print(df)
# message_text = "test"  # 推送消息，纯文本为字符串，富文本格式见飞书开发者社区
message_text = send_msg
feishu = FeiShuMSG(token=webhook, text=message_text)
feishu.send_msg_info()

